package com.jhcee.jhgk.inform.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.jhcee.jhgk.inform.domain.model.WechatConfigUrl;
import com.jhcee.jhgk.inform.domain.model.WechatMessageModel;
import com.jhcee.jhgk.inform.domain.model.WechatTextModel;
import com.jhcee.jhgk.inform.service.WechatConfigInfoService;
import com.jhgk.jhcee.basics.mapper.WechatConfigInfoMapper;
import com.jhgk.jhcee.basics.model.WechatConfigInfo;
import com.jhgk.jhcee.basics.model.WechatConfigInfoExample;
import com.jhgk.jhcee.common.enums.BasisResultEnum;
import com.jhgk.jhcee.common.resultful.BasisResultful;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: xiezhongping
 * @description:
 * @date: 2021/8/29 15:00.
 */
@Service(value = "WechatConfigInfoServiceImpl")
public class WechatConfigInfoServiceImpl implements WechatConfigInfoService {

    @Autowired
    WechatConfigInfoMapper wechatConfigInfoMapper;

    @Override
    public BasisResultful updateToken(String appid) {
        if (StringUtils.isBlank(appid)) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        WechatConfigInfoExample example = new WechatConfigInfoExample();
        WechatConfigInfoExample.Criteria criteria = example.createCriteria();
        criteria.andAppidEqualTo(appid);
        List<WechatConfigInfo> wechatConfigInfos = wechatConfigInfoMapper.selectByExample(example);
        if (wechatConfigInfos.isEmpty()) {
            return BasisResultful.fail(BasisResultEnum.QUERY_IS_NULL);
        }
        WechatConfigInfo wechatConfigInfo = wechatConfigInfos.get(0);
        String result = HttpUtil.get(WechatConfigUrl.UPDATE_TOKEN.replace("ID", appid).replace("SECRET", wechatConfigInfo.getSecret()));
        JSONObject jsonObject = JSONObject.parseObject(result);
        wechatConfigInfo.setErrcode(String.valueOf(jsonObject.get("errcode")));
        wechatConfigInfo.setErrmsg(jsonObject.getString("errmsg"));
        wechatConfigInfo.setToken(jsonObject.getString("access_token"));
        wechatConfigInfo.setExpiresIn((Integer) jsonObject.get("expires_in"));
        int updateStatus = wechatConfigInfoMapper.updateByPrimaryKey(wechatConfigInfo);
        if (updateStatus > 0) {
            return BasisResultful.success(BasisResultEnum.SUCCESS);
        }
        return BasisResultful.success(BasisResultEnum.FAIL);
    }

    @Override
    public BasisResultful<WechatConfigInfo> getToken(String appid) {
        if (StringUtils.isBlank(appid)) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        WechatConfigInfoExample example = new WechatConfigInfoExample();
        WechatConfigInfoExample.Criteria criteria = example.createCriteria();
        criteria.andAppidEqualTo(appid);
        List<WechatConfigInfo> wechatConfigInfos = wechatConfigInfoMapper.selectByExample(example);
        if (wechatConfigInfos.isEmpty()) {
            return BasisResultful.fail(BasisResultEnum.QUERY_IS_NULL);
        }
        return BasisResultful.success(wechatConfigInfos.get(0));
    }

    @Override
    public BasisResultful sendAllTextMessage(int agentId, String text) {
        if (StringUtils.isBlank(text)) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        WechatMessageModel messageModel = new WechatMessageModel();
        WechatTextModel textModel = new WechatTextModel();
        textModel.setContent(text);
        messageModel.setTouser("@all");
        messageModel.setToparty("@all");
        messageModel.setTotag("@all");
        messageModel.setMsgtype("text");
        messageModel.setAgentid(agentId);
        messageModel.setText(textModel);
        messageModel.setSafe(0);
        messageModel.setEnable_id_trans(0);
        messageModel.setEnable_duplicate_check(0);
        messageModel.setDuplicate_check_interval(1800);
        BasisResultful<WechatConfigInfo> token = getToken(WechatConfigUrl.QYAPPID);
        if (token.getCode() != 0) {
            return token;
        }
        String result = HttpUtil.post(WechatConfigUrl.SEND_MESSAGE.replace("ACCESS_TOKEN", token.getData().getToken()), JSONObject.toJSONString(messageModel));
        JSONObject jsonObject = JSONObject.parseObject(result);
        if ("0".equals(String.valueOf(jsonObject.get("errcode")))) {
            return BasisResultful.success();
        }
        return BasisResultful.fail();
    }
}
